﻿namespace ClassLibrary.DataLayer.DataObjects
{
    using ClassLibrary.BusinessLayer.BusinessObjects;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;

    public class ProductDAO : IProductDAO
    {
        public bool DeleteProduct(Product clsProduct)
        {
            return clsProduct.Save("D");
        }

        public IList<Product> GetProduct(Product clsProduct)
        {
            DataTable dt = Db.GetDataTable(clsProduct.CreateGetCommandParameters());
            clsProduct.LoadDataToCollection(dt.Rows[0]);
            if (dt.Rows[0] == null)
            {
                throw new Exception("Product doesn't exist");
            }
            IList<Product> list = new List<Product>();
            list.Add(clsProduct);
            return list;
        }

        public IList<Product> GetProduct(string product)
        {
            Product clsProduct = new Product();
            clsProduct.Model = product;
            return this.GetProduct(clsProduct);
        }

        public IList<Product> GetsProduct()
        {
            Product clsProduct = new Product();
            IDataReader dtReader = Db.GetDataReader(clsProduct.CreateGetsCommandParameters());
            IList<Product> list = new List<Product>();
            while (dtReader.Read())
            {
                clsProduct = new Product();
                clsProduct.LoadDataToCollection(dtReader);
                list.Add(clsProduct);
            }
            return list;
        }

        public DataTable GetsProduct(string SPName, DbParameter[] param)
        {
            Product clsProduct = new Product();
            return Db.GetDataTable(clsProduct.GetsDataReader(SPName, param));
        }

        public bool InsertProduct(Product clsProduct)
        {
            return clsProduct.Save("I");
        }

        public int InsertProductReturnIdentity(Product clsProduct)
        {
            return clsProduct.Save("I", true);
        }

        public string InsertProductReturnOutput(Product clsProduct)
        {
            return clsProduct.Save("I", "@ProductID");
        }

        public IList<Product> ListProduct(Product clsProduct)
        {
            IDataReader dtReader = Db.GetDataReader(clsProduct.CreateListCommandParameters());
            IList<Product> list = new List<Product>();
            while (dtReader.Read())
            {
                Product clsProducts = new Product();
                clsProducts.LoadDataToCollection(dtReader);
                list.Add(clsProducts);
            }
            return list;
        }

        public IList<Product> ListProduct(string product)
        {
            Product clsProduct = new Product();
            clsProduct.Model = product;
            return this.ListProduct(clsProduct);
        }

        public bool UpdateProduct(Product clsProduct)
        {
            return clsProduct.Save("U");
        }

        public int UpdateProduct(string SPName, DbParameter[] param)
        {
            Product clsProduct = new Product();
            return Db.ExecuteNonQuery(clsProduct.UpdateQuery(SPName, param));
        }
    }
}

